<?
session_start();
include 'php/connect.php';
$userName =  mysql_real_escape_string($_POST['userName']);


If($userName != ""){
	//SOMEONE ELSES PAGE
	$isOwn = false;
}elseif (isset($_SESSION['userId'])){
	//MY OWN PAGE IF LOGGED IN
	$isOwn = true;
	$userName = $_SESSION['userId'];
}else{
	//NOT LOGGED IN AND NO USER SPECIFIED
	return "Please login to see your specs!";
}

if ($userName != ""){

	$output = " <script>				
					$(document).ready(function(){
						$('.delete-spec').click(function(e) {	
							var clicked = jQuery(e.target);
							var id = clicked.attr('id');
							deleteSpec(id);
						});
						$('.publish-spec').click(function(e) {	
							var clicked = jQuery(e.target);
							var id = clicked.attr('id');
							publishSpec(id);
						});
						$('.unpublish-spec').click(function(e) {	
							var clicked = jQuery(e.target);
							var id = clicked.attr('id');
							unPublishSpec(id);
						});
						$('.show-spec').click(function(e) {	
							var clicked = jQuery(e.target);
							var id = clicked.attr('id');
							editSpec(id);
						});
						$('.edit-spec').click(function(e) {	
							var clicked = jQuery(e.target);
							var id = clicked.attr('id');
							editSpec(id);
						});
						$('.calc-link').click(function(e) {	
							params = {}
							url = 'calculator';
							show = '#/'+url;
							loadURL(url, params, show);
						});
					});
				</script>
				<table id='me_specs' class='content-subtitle-table'>";
				
					
				$abfrage = "SELECT spec.specId
					   ,spec.userId
					   ,spec.layout
					   ,spec.jobId
					   ,spec.level
					   ,job.tri as jobTri
					   ,job.name as jobName
					   ,spec.description
					   ,spec.public
					   ,spec.cdate
					   ,spec.udate
				FROM ((
				ffxivc_specs as spec 
				LEFT JOIN 
				ffxivc_jobs as job
				ON job.jobId = spec.jobId)
				LEFT JOIN ffxivc_users as user
				ON user.userId = spec.userId) ";
				
				if(!$isOwn){
					$abfrage .= "WHERE user.userName = \"".$userName."\" AND spec.public = 'Y'";
				}else{
					$abfrage .= "WHERE spec.userId = \"".$_SESSION['userId']."\"";
				}
				
				$max = 7;
				$res = mysql_query($abfrage);
				$num = mysql_num_rows($res);	
				$i = 1;
				$rest = $max - $num;

				while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
				
					if($i==1){
						$output .= "<script>loadSpecDetail(".$row['specId']."); loadSpecComments(".$row['specId'].", 5); </script>";
					}
				
				
					$output .= "<tr id='tr_".$row['specId']."' title='".$row['description']."' class='spec-row tr-table border-bottom' name='".$row['specId']."'>
							<td class='td-table vertical-middle pad-5 cur-point'><div class='normal-text bold-text'>".$row['level']."</div></td>
							<td class='td-table vertical-middle pad-5 cur-point'><img class='cur-point' src='media/jobIcons/B_".$row['jobTri'].".png' /></td>
							<td class='td-table vertical-middle pad-5 width-90 pad-lr-20 cur-point'><div class='normal-text bold-text'>".$row['jobName']."</div></td>
							<td id='publish-spec-toggle' class='td-table vertical-middle pad-5' name='no'>";
							
					if($isOwn){
							$output .= "<div id='publish-link_".$row['specId']."' class='publish-link pad-5 fl-left' style='width: 46px;'> ";
						if($row['public'] == 'Y'){
							$output .= 		"<a class='unpublish-spec publish-link font-10 fl-right' name='no' id='".$row['specId']."' style='cursor:pointer;'>UnPublish";
						}else{
							$output .= 		"<a class='publish-spec publish-link font-10 fl-right' name='no' id='".$row['specId']."' style='cursor:pointer;'>Publish";
						}
						
						
						$output .=	"</a></div>
									<div name='no' class='normal-text bold-text grey-text pad-t-7 fl-left pad-5'>*</div>
									<a id='".$row['specId']."' class='edit-spec fl-left pad-5 font-10 cur-point' name='no'>Edit</a>
									<div name='no' class='normal-text bold-text grey-text pad-t-7 fl-left pad-5'>*</div>
									<a id='".$row['specId']."' class='delete-spec fl-left pad-5 font-10 cur-point' name='no'>Delete</a>";

					}else{
						$output .= "<a id='".$row['specId']."' class='show-spec fl-right horizontal-right pad-5 font-10' style='width: 150px;cursor:pointer;' name='no' >Show</a>";
					}
						$output .=" </td>
						</tr>";
						
					if($i == $max){
						break;
					}	
					
					$i++;
					
				}
				if ($rest > 0){
				
					for ($i = 1; $i <= $rest; $i++) {
						
						
						if($isOwn){
							$output .= "<tr title='Create a spec' class='calc-link tr-table cur-point ";
						}else{
							$output .= "<tr class='tr-table ";
						}
						if(!($num == 0 && $i == 1))	{
							$output .= "border-top";
						}
						$output .= "'>
								<td class='td-table vertical-middle pad-5'><div class='normal-text bold-text'></div></td>
								<td class='td-table vertical-middle pad-5'><div class='empty-slot'></div></td>
								<td class='td-table vertical-middle pad-5 width-200 pad-lr-20' colspan='2'><div class='normal-text bold-text'>Empty slot</div></td>
							</tr>";
							
					}
				}

				mysql_free_result($res);
			
		$output .= "	</table>
						<script type='text/javascript'><!--

						$('.spec-row').click(function(e) {
							var clicked = $(e.target);
							var tr = $(this).closest('tr');
							var specId = tr.attr('name');
							if (clicked.attr('name') != 'no'){
								loadSpecDetail(specId);
								loadSpecComments(specId, 5);
							}
						});
						
					//-->
				</script>";

	echo  $output;
}

?>